<template>
  <div class="goods">
    <van-list
      class="goods-list"
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
      offset="0"
    >
      <div
        class="goods-list-item"
        v-for="(good,index) in goods"
        :key="index"
        @click="goDetail('/home/goodsinfo',good.id)"
      >
        <div class="img-box">
          <img v-lazy="good.img_url">
        </div>
        <h2 class="title">{{good.title}}</h2>
        <div class="price-wrap">
          <div class="desc"> 主动降噪 </div>
          <p class="price"> ￥<b>{{good.sell_price}}</b></p>
        </div>
      </div>
    </van-list>
  </div>
</template>
<script>
// import mixins from '@/mixins'
export default {
  // mixins: [mixins],
  data() {
    return {
      goods: [],
      page: 0,
      pageSize: 4,
      loading: false,
      finished: false,
      total: 0
    }
  },
  created() {
    // this.getGoods()
    // console.log('component create')
  },
  methods: {
    onLoad() {
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      setTimeout(async () => {
        await this.getGoods()
        // 加载状态结束
        this.loading = false
        // 数据全部加载完成
        if (this.goods.length >= this.total) {
          this.finished = true
        }
      }, 500)
    },
    async getGoods() {
      ++this.page
      const res = await this.$http.getGoods({ page: this.page, pageSize: this.pageSize })
      this.goods = this.goods.concat(res.data.message)
      this.total = res.data.count
    }
  }
}
</script>
<style lang="scss">
.goods {
  padding: 10px;
  .goods-list {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    .goods-list-item {
      border: 1px solid #eee;
      width: 49%;
      margin-bottom: 10px;
      padding: 8px;
      box-sizing: border-box;
      text-align: center;
      .img-box {
        text-align: center;
        width: 170px;
        height: 170px;
        img {
          width: 170px;
          height: 170px;
          display: block;
        }
      }
      .title {
        font-size: 14px;
      }
      .price-wrap {
        font-size: 12px;
        .desc {
          color: #666;
        }
        .price {
          color: red;
        }
      }
    }
  }
  .van-list__error-text,
  .van-list__finished-text,
  .van-list__loading {
    width: 100%;
  }
}
</style>